from ..tools.math.matrix import Matrix
from . import api
from flask import jsonify, request
import numpy as np

@api.route('/math/matrix', methods=['POST'])
def matrix():
    data = request.json
    m1: str = data.get('m1')
    m2: str = data.get('m2')
    m1 = Matrix(str_to_matrix(m1))
    m2 = Matrix(str_to_matrix(m2))
    m3 = m1 * m2
    return jsonify(result=repr(m3))


def str_to_matrix(line: str):
    lines = line.splitlines()
    str_matrix = list(map(lambda x: x.split(' '), lines))
    return list(map(str_to_int, str_matrix))

def str_to_int(line: list):
    return list(map(lambda x: float(x), line)) 